package org.kfive.nova.io.netty.buffer;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import java.nio.charset.Charset;

/**
 * Created by admin on 2017-05-19.
 */
public class NettyBufferTest {

    public void testBuffer(){
        ByteBuf heapBuf = Unpooled.wrappedBuffer("hello world".getBytes());
        /*heapBuf.readByte();
        System.out.println(heapBuf.readableBytes());*/
        ByteBuf readBuf = Unpooled.buffer(5);
        heapBuf.readBytes(readBuf);
        System.out.println("headBuf.readindex:"+heapBuf.readerIndex());
        System.out.println("readBuf.writeindex:"+readBuf.writerIndex());
        while (heapBuf.isReadable()){
            System.out.println((char)heapBuf.readByte());
        }
    }

    public void testCopyByteBuf(){
        Charset utf8 = Charset.forName("UTF-8");
        ByteBuf buf = Unpooled.copiedBuffer("Netty in Action rocks!",utf8);
        ByteBuf sliced = buf.slice(0,15);
        System.out.println(sliced.toString(utf8));
        ByteBuf copyed = buf.copy(0,15);
        buf.setByte(0,(byte)'j');
        buf.array()[15]='a';
        System.out.println(buf.toString(utf8));
        System.out.println(copyed.toString(utf8));
        assert buf.getByte(0)==sliced.getByte(0);
        assert buf.getByte(0)==copyed.getByte(0);

    }


    public static void main(String[] args) {
        NettyBufferTest test = new NettyBufferTest();
        test.testCopyByteBuf();
    }
}
